var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    nail: [0, 0, 0],
    naillist: ['未联系', '无需继续联系', '继续跟进',],
    yxdnail: [0, 0, 0],
    currentTab: 0,
    attendeeid: '',
    showModal: false,
    luzsshow: false,
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      complete: (res) => { },
    });
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  toBadge: function () {
    util.toBadge()
  },
  toChangeShow: function () {
    var that = this
    that.setData({
      luzsshow: false,
    })
  },
  sczl: function (e) {
    console.log(e)
    var materialid = e.currentTarget.dataset.zlid
    var that = this
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/materials/collect'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["materialid"] = materialid
      k["u"] = u
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'materialid' + materialid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  luzs: function () {
    var that = this;
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var toid = that.data.attendeeid
    console.log('toid=' + toid)
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/exhibitors/contactme'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["toid"] = toid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + 'toid' + toid + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          if (mesg.code == 200) {
            that.setData({
              luzsshow: true,
              showModal: false
            })
          } else {
            wx.showToast({
              title: mesg.message,
              icon: 'none',
              duration: 1500
            })
          }

        }, fail: res => {
          console.log(res, "error")
        }

      })


    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  //查看资料跳转receiveInfolj
  toReceiveInfolj(e) {
    console.log(e)
    var urlmesg = e.currentTarget.dataset.urlmesg
    wx.navigateTo({
      url: '../receiveInfo/receiveInfolj?urlmesg=' + urlmesg,
    })
  },
  onShareAppMessage: function (res) {
    var that = this
    that.setData({
      showModal: false
    })
    var fxurl = 'pages/receiveInfo/receiveInfolj?urlmesg='+that.data.fxurl
    console.log('fxurl=' + fxurl)
    if (res.from === 'button') { }
    return {
      title: '资料分享',
      path: fxurl,
      success: function (res) { }
    }
  },
  preventTouchMove: function () {

  },
  edit: function () {
    //点击笔记跳转保存笔记页
    var that = this
    var attendeeid = that.data.attendeeid
    wx.navigateTo({
      url: '../myNotes/edit?attendeeid=' + attendeeid,
    })
  },
  inputs: function () {
    //点击笔记跳转保存笔记页
    var that = this
    var attendeeid = that.data.attendeeid
    var notecontent = that.data.notecontent
    wx.navigateTo({
      url: '../myNotes/myNotes?attendeeid=' + attendeeid + '&notecontent=' + notecontent,
    })
  },
  go: function () {
    this.setData({
      showModal: false
    })
  },
  onShow: function () { 
    //加载自定义标签
    var that = this
    var attendeeid = that.data.attendeeid
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var cururl = app.globalData.picoaddress + '/attendee/tags/list'
    var t = {}
    t["v"] = '1.0.0'
    t["client"] = 'ios'
    t["timestamp"] = timestamp
    t["lan"] = 'cn'
    t["exhibitionid"] = exhibitionid
    t["attendeeid"] = attendeeid
    t["u"] = u
    var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.zhMD5(sign)
    console.log(curb64)
    t["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: cururl,
      method: 'post',
      data: t,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data

        that.setData({
          tagsDetail: curdata
        });

      }, fail: res => {
        console.log(res, "error")
      }
    })
  },
  onLoad: function (options) {
    var that = this
    // 页面初始化 options为页面跳转所带来的参数
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Contact details'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '联系人详情'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    var attendeeid = options.attendeeid
    console.log('attendeeid=' + attendeeid)
    that.setData({
      attendeeid: attendeeid
    });
    //调用pico个人信息详情
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = app.globalData.picoaddress + '/attendee/contacts/detail'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["attendeeid"] = attendeeid
    k["u"] = u
    var sign = app.globalData.picotoken + 'attendeeid' + attendeeid+'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.hexMD5(sign)
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data
        var fullname = curdata.fullname
        var status = curdata.status
        var yxdArray = curdata.intentionalitys
        var curyxdnail = [0,0,0]
        for (var i in yxdArray) {
          var intentionalityid = yxdArray[i].intentionalityid
          if (yxdArray[i].selected) {
            curyxdnail[intentionalityid] = '1'
          }
        }
        that.setData({
          yxdnail: curyxdnail
        });
        //设置标签
        var curnail = [0,0,0]
        curnail[status]='1'
        that.setData({
          nail: curnail
        });
        that.setData({
          personDetail: curdata,
          fullname:fullname
        });
        that.setData({
          notecontent: curdata.notecontent
        });

      }, fail: res => {
        console.log(res, "error")
      }
    })
    //加载自定义标签
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var cururl = app.globalData.picoaddress + '/attendee/tags/list'
    var t = {}
    t["v"] = '1.0.0'
    t["client"] = 'ios'
    t["timestamp"] = timestamp
    t["lan"] = 'cn'
    t["exhibitionid"] = exhibitionid
    t["attendeeid"] = attendeeid
    t["u"] = u
    var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.zhMD5(sign)
    console.log(curb64)
    t["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: cururl,
      method: 'post',
      data: t,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data

        that.setData({
          tagsDetail: curdata
        });

      }, fail: res => {
        console.log(res, "error")
      }
    })
    //获取当前联系人资料
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var cururl = app.globalData.picoaddress + '/attendee/contacts/detail'
    var m = {}
    m["v"] = '1.0.0'
    m["client"] = 'ios'
    m["timestamp"] = timestamp
    m["lan"] = 'cn'
    m["exhibitionid"] = exhibitionid
    //联系人id
    m["attendeeid"] = attendeeid
    m["u"] = u
    var sign = app.globalData.picotoken + 'attendeeid' + attendeeid+ 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.zhMD5(sign)
    console.log(curb64)
    m["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: cururl,
      method: 'post',
      data: m,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data

        that.setData({
          'zlmesgDetail': curdata
        });

      }, fail: res => {
        console.log(res, "error")
      }
    })
  },
  tagchange: function (e) {
    var that = this;
    var attendeeid = that.data.attendeeid
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var tagMesg = e.currentTarget.dataset.tagmesg;
    var tagArray = tagMesg.split("-");
    var tagid = tagArray[0]
    var sfxz = tagArray[1]
    //根据sfxz判断增加联系人标签还是删除联系人标签
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url =''
    if (sfxz=='false'){
      url = app.globalData.picoaddress + '/attendee/tags/settag'
    } 
    if (sfxz == 'true') {
      url = app.globalData.picoaddress + '/attendee/tags/del'
    } 
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["attendeeid"] = attendeeid
    k["tagid"] = tagid
    k["u"] = u
    var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'tagid' + tagid+ 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.hexMD5(sign)
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
      }, fail: res => {
        console.log(res, "error")
      }

    })
    //重新加载自定义标签
    //加载自定义标签
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var cururl = app.globalData.picoaddress + '/attendee/tags/list'
    var t = {}
    t["v"] = '1.0.0'
    t["client"] = 'ios'
    t["timestamp"] = timestamp
    t["lan"] = 'cn'
    t["exhibitionid"] = exhibitionid
    t["attendeeid"] = attendeeid
    t["u"] = u
    var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.zhMD5(sign)
    console.log(curb64)
    t["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: cururl,
      method: 'post',
      data: t,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data

        that.setData({
          tagsDetail: curdata
        });

      }, fail: res => {
        console.log(res, "error")
      }
    })
   
  },
  toMesgDetail(e) {
    console.log(e)
    var mesg = e.currentTarget.dataset.tagid
    var attendeeid = (mesg.split('-'))[0]
    var nickname = (mesg.split('-'))[1]
    wx.navigateTo({
      url: '../contactsModule/chat?attendeeid=' + attendeeid + '&nickname=' + nickname,
    })
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  makePhone: function (e) {
    var that = this;
    var phoneNumber = that.data.personDetail.mobileno 
    console.log("phoneNumber=" + phoneNumber)
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  hide: function () {
    var that = this;
    this.setData({
      showModal: false
    })
  },
  meet: function () {
    var that = this
    var toid = that.data.attendeeid
    var fullname = that.data.fullname
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var cururl = app.globalData.picoaddress + '/attendee/exhibitors/appointment'
    var t = {}
    t["v"] = '1.0.0'
    t["client"] = 'ios'
    t["timestamp"] = timestamp
    t["lan"] = 'cn'
    t["exhibitionid"] = exhibitionid
    t["toid"] = toid
    t["u"] = u
    var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + 'toid' + toid + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.zhMD5(sign)
    console.log(curb64)
    t["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: cururl,
      method: 'post',
      data: t,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        if (mesg.code == 200) {
          wx.showToast({
            title: '约见展商消息已成功发出，等待展商确认',
            icon: 'none',
            duration: 2000
          })
          wx.navigateTo({
            url: '../contactsModule/chat?attendeeid=' + toid+'&nickname='+fullname,
          })
        } else {
          wx.showToast({
            title: mesg.message,
            icon: 'none',
            duration: 2000
          })
        }
      }, fail: res => {
        console.log(res, "error")
      }
    })
    this.setData({
      showModal: false
    })
  },
  delrelation: function () {
    var that = this;
    var that = this
    var attendeeid = that.data.attendeeid
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var cururl = app.globalData.picoaddress + '/attendee/contacts/del'
    var t = {}
    t["v"] = '1.0.0'
    t["client"] = 'ios'
    t["timestamp"] = timestamp
    t["lan"] = 'cn'
    t["exhibitionid"] = exhibitionid
    t["attendeeid"] = attendeeid
    t["u"] = u
    var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.zhMD5(sign)
    console.log(curb64)
    t["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: cururl,
      method: 'post',
      data: t,
      success(res) {
        console.log(res)
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        if (mesg.code == 200) {
          wx.navigateBack({
            success: function () {
              console.log('成功！')
            }
          })
        }
      }, fail: res => {
        console.log(res, "error")
        
      }
    })
    this.setData({
      showModal: false
    })
  },
  nailchoice(e) {
    var that = this;
    var pages = getCurrentPages();
    //var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    
    //直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
    
    that.choice(that, 'nail', e)
    var nail = that.data.nail
    var tmpstr = ''
    console.log(nail)

    for (var i in nail) {
      if (nail[i] == 1) {
        tmpstr += that.data.naillist[i] + ","
      }
    }
    console.log('tmpstr=' + tmpstr)
    
    //调用接口设置状态
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var attendeeid = that.data.attendeeid
    var status = e.currentTarget.dataset.it
    var atidandstus = attendeeid+'-'+status 
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/contacts/setstate'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["attendeeid"] = attendeeid
      k["status"] = status
      var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'status' + status+ 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          prevPage.setData({
            szattendeeid: atidandstus
          });
        }, fail: res => {
          console.log(res, "error")
        }
      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }

  },
  choice(that, key, e) {
    //设置其他为0 
    that.setData({
      nail: [0, 0, 0]
    })
    var nail = that.data[key]
    console.log('nail=' + nail)
    var it = e.currentTarget.dataset.it
    //其他设置为0

    var data = key + '[' + it + ']'
    console.log('data=' + data)
    console.log('nail[it]=' + nail[it])
    console.log('[data]=' + [data])
    if (!nail[it] || nail[it] == 0) {
      that.setData({
        [data]: 1
      })
    } else if (nail[it] == 1) {
      that.setData({
        [data]: 0
      })
    }
  },
  yxdnailchoice(e) {
    var that = this;
    that.yxdchoice(that, 'yxdnail', e)
    var yxdenail = that.data.nail
    //调用接口设置状态
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var attendeeid = that.data.attendeeid
    var intentionid = e.currentTarget.dataset.it
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/contacts/setintention'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["attendeeid"] = attendeeid
      k["intentionid"] = intentionid
      var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'intentionid' + intentionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
        }, fail: res => {
          console.log(res, "error")
        }
      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }

  },
  yxdchoice(that, key, e) {
    //设置其他为0 
    that.setData({
      yxdnail: [0, 0, 0]
    })
    var yxdnail = that.data[key]
    var it = e.currentTarget.dataset.it
    //其他设置为0

    var data = key + '[' + it + ']'
    if (!yxdnail[it] || yxdnail[it] == 0) {
      that.setData({
        [data]: 1
      })
    } else if (yxdnail[it] == 1) {
      that.setData({
        [data]: 0
      })
    }
  }

})